package com.oauth2.controller;


import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.oauth2.Enum.GrantTypeEnum;
import com.oauth2.Enum.ResponseErrorEnum;
import com.oauth2.dto.AccessTokenResponse;
import com.oauth2.dto.OauthError;
import com.oauth2.dto.Result;
import com.oauth2.entity.GatewayUser;
import com.oauth2.service.OauthService;



@RestController
public class OauthController {
	
	@Autowired
	OauthService oauthService;
	
	@RequestMapping("/token")
	public ResponseEntity<String> token(HttpServletRequest request){
		String grant_type=request.getParameter("grant_type");
		String client_id=request.getParameter("client_id");
		String client_secret=request.getParameter("client_secret");
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		String refreshToken=request.getParameter("refresh_token");
		//String scope=request.getParameter("scope");
		OauthError error=new OauthError();
		AccessTokenResponse tokenResponse=new AccessTokenResponse();
		//判断grant_type合法性
		if(GrantTypeEnum.getGrantTypeEnumByCode(grant_type)==null){
			error.setError(ResponseErrorEnum.UNSUPPORTED_GRANT_TYPE.getError());
			error.setError_description(ResponseErrorEnum.UNSUPPORTED_GRANT_TYPE.getError_description());
			return new ResponseEntity<String>(JSON.toJSONString(error),HttpStatus.OK);
		}
		//客户端模式
		if(grant_type.equals(GrantTypeEnum.CLIENT_CREDENTIALS.getCode())){
			if(client_id==null||client_secret==null){
				error.setError(ResponseErrorEnum.INVALID_REQUEST.getError());
				error.setError_description(ResponseErrorEnum.INVALID_REQUEST.getError_description());
				return new ResponseEntity<String>(JSON.toJSONString(error),HttpStatus.OK);
			}
			
			tokenResponse=oauthService.clientCredentials(client_id, client_secret);
			if(tokenResponse==null){
				error.setError(ResponseErrorEnum.INVALID_CLIENT.getError());
				error.setError_description(ResponseErrorEnum.INVALID_CLIENT.getError_description());
				return new ResponseEntity<String>(JSON.toJSONString(error),HttpStatus.UNAUTHORIZED);
			}
			
		}
		//用户名密码模式
		if(grant_type.equals(GrantTypeEnum.PASSWORD.getCode())){
			
			if(username==null||password==null||client_secret==null||client_id==null){
				error.setError(ResponseErrorEnum.INVALID_REQUEST.getError());
				error.setError_description(ResponseErrorEnum.INVALID_REQUEST.getError_description());
				return new ResponseEntity<String>(JSON.toJSONString(error),HttpStatus.OK);
			}
			tokenResponse=oauthService.passwordGrant(username, password, client_id, client_secret);
			if(tokenResponse==null){
				error.setError(ResponseErrorEnum.INVALID_CLIENT.getError());
				error.setError_description(ResponseErrorEnum.INVALID_CLIENT.getError_description());
				return new ResponseEntity<String>(JSON.toJSONString(error),HttpStatus.UNAUTHORIZED);
			}
		}
		//刷新token模式
		if(grant_type.equals(GrantTypeEnum.REFRESH_TOKEN.getCode())){
			if(client_id==null||client_secret==null||refreshToken==null){
				error.setError(ResponseErrorEnum.INVALID_REQUEST.getError());
				error.setError_description(ResponseErrorEnum.INVALID_REQUEST.getError_description());
				return new ResponseEntity<String>(JSON.toJSONString(error),HttpStatus.OK);
			}
			tokenResponse=oauthService.refreshToken(client_id, client_secret, refreshToken);
			if(tokenResponse==null){
				error.setError(ResponseErrorEnum.INVALID_CLIENT.getError());
				error.setError_description(ResponseErrorEnum.INVALID_CLIENT.getError_description());
				return new ResponseEntity<String>(JSON.toJSONString(error),HttpStatus.UNAUTHORIZED);
			}
		}
		
		return new ResponseEntity<String>(JSON.toJSONString(tokenResponse),HttpStatus.OK);
	}
	@RequestMapping("/validata")
	public ResponseEntity<Object> validataToken(@RequestParam(value="access_token",required=false)String access_token){
		Result<GatewayUser> res=new Result<GatewayUser>();
		GatewayUser entity=oauthService.validataToken(access_token);
		OauthError error=new OauthError();
		if(access_token==null){
			error.setError(ResponseErrorEnum.INVALID_TOKEN.getError());
			error.setError_description(ResponseErrorEnum.INVALID_TOKEN.getError_description());
			return new ResponseEntity<Object>(error,HttpStatus.OK);
		}
		if(entity!=null){
			res.setResult(entity);
			return new ResponseEntity<Object>(res,HttpStatus.OK);
		}else{
			error.setError(ResponseErrorEnum.INVALID_TOKEN.getError());
			error.setError_description(ResponseErrorEnum.INVALID_TOKEN.getError_description());
			return new ResponseEntity<Object>(error,HttpStatus.OK);
		}
	}

}
